package com.tjetc.view.teacherview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Quickresponse;
import com.zhiyun.domain.Student;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.QuickResponseService;
import com.zhiyun.service.StudentService;
import com.zhiyun.vo.Result;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Answer {
    StudentService studentService=CilentProxy.getProxyInstance(StudentService.class);
    CourseService courseService=CilentProxy.getProxyInstance(CourseService.class);
    QuickResponseService quickResponseService= CilentProxy.getProxyInstance(QuickResponseService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
       while (true){
           System.out.println("抢答");
           System.out.println("1. 抢答列表");
           System.out.println("2. 发布抢答");
           System.out.println("0. 返回上一级");
           int i = scanner.nextInt();
           switch (i){
               case 1:
                   allAnswer(course);
                   break;
               case 2:
                   addAnswer(course);
                   break;
               case 0:
                   return;
               default:
                   break;
           }

       }
    }

    private void allAnswer(Course course) {
        while (true){
            Result result = quickResponseService.allQuick(course.getId());
            if (result.getCode()==200){
                System.out.println("加载抢答列表成功");
                List<Quickresponse> obj = (List<Quickresponse>) result.getObj();
                for (Quickresponse quickresponse : obj) {
                    Result byId = courseService.findById(quickresponse.getCourseId());
                    if (byId.getCode()==200){
                        Course obj1 = (Course) byId.getObj();
                        if (obj1!=null){
                            Result result1 = studentService.myInfo(quickresponse.getStudentId());
                            if (result.getCode()==200){
                                Student obj2 = (Student) result1.getObj();
                                if (obj2!=null){
                                    String truename = obj2.getTruename();
                                    System.out.println("id:"+quickresponse.getId()+"   问题"+quickresponse.getQuestion()+"     课程名称"+obj1.getName()+"   学生id:"+quickresponse.getStudentId()+"   学生姓名:" + truename);
                                }

                            }

                        }
                    }

                }
            }else {
                System.out.println("加载抢答列表失败");
            }
            break;
        }
    }

    private void addAnswer(Course course) {
        while (true){
            System.out.println("请输入问题");
            String next = scanner.next();
            Result result = quickResponseService.insertQuick(new Quickresponse(null, next, course.getId(), null, new Date()));
            if (result.getCode()==200){
                System.out.println("创建抢答成功");
            }else {
                System.out.println("创建抢答失败");
            }
            break;
        }
    }
}
